<?php
class Folder
{
    public static function Group($path,$chunkCount=500,$method='number')
    {
        #检查文件夹是否存在，不存在创建
        self::Create($path);

        #开始分组处理
        $name=NULL;    
        //计算子文件夹名称
        if($method=='number')
        {
            if(self::IsEmpty($path)){
                $name='1';
            }
            else
            {
                $lastFolderName=self::LastFolderName($path,'is_numeric');                
                $folderCount=self::FolderCount($path);
                if(self::FileCount($path.'/'.$lastFolderName)>=$chunkCount)
                {
                    $name=$folderCount+1;
                    $name=self::Create_Recursion($path,$name,NameFactory::Create('Linear'));
                }
                else
                {
                    $name=$lastFolderName;
                }
            }
        }
        else{
            $name=date('Ymd').time().rand(1,1000);
        }
        
        //创建目录
        $path=$path.'/'.$name;
        if(!is_dir($path)){
            mkdir($path);
        }
        return $name;
    }

    public static function Create($path)
    {
        if(!is_dir($path))
        {
            $path=explode('/',str_replace('\\','/',$path));
            for($i=0;$i<count($path);$i++)
            {
                $temp=join('/',array_slice($path,0,$i+1));
                if(!is_dir($temp))
                {
                    mkdir($temp);
                    //echo $temp.'<br/>';
                }                                
            }       
        }        
    }

    public static function IsEmpty($path)
    {
        $handle = @opendir($path);
        while (($file = @readdir($handle)) !== FALSE)
        {
            if ($file != "." && $file != "..")
            {
                @closedir($handle);
                return FALSE;
            }
        }
        @closedir($handle);
        return TRUE;
    }

    public static function LastFolderName($path,$callback=NULL)
    {
        $temp=array();
        foreach(scandir($path) as $file)
        {
            if(is_dir($path.'/'.$file))
            {                
                if(call_user_func_array($callback,array($file))) $temp[]=$file;
            }
        }        
        return @$temp[count($temp)-1];
    }

    public static function FolderCount($path)
    {
        $count=0;
        $handle=@opendir($path);
        while(($file=@readdir($handle))!==FALSE)
        {
            if($file!="."&&$file!="..")
            {
                if(is_dir($path.'/'.$file)){
                     $count=$count+1;
                }
            }
        }
        return $count;
    }

    public static function FileCount($path)
    {
        $count=0;
        $handle=@opendir($path);
        while(($file=@readdir($handle))!==FALSE)
        {
            if($file!="."&&$file!="..")
            {
                if(is_file($path.'/'.$file)){
                     $count=$count+1;
                }
            }
        }
        return $count;
    }

    public static function Create_Recursion($path,$name,$object)
    {
        if(is_dir($path.'/'.$name))
        {
            self::Create_Recursion($path,$object->Get($name),$object);
        }
        else
        {
            return $name;
        }
    }
}
?>